
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="zh">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>34.2. msvcrt --- Useful routines from the MS VC++ runtime &#8212; Python 3.6.6rc1 文档</title>
    <link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript" src="../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/sidebar.js"></script>
    <link rel="search" type="application/opensearchdescription+xml"
          title="在 Python 3.6.6rc1 文档 中搜索"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="关于这些文档" href="../about.html" />
    <link rel="index" title="索引" href="../genindex.html" />
    <link rel="search" title="搜索" href="../search.html" />
    <link rel="copyright" title="Copyright" href="../copyright.html" />
    <link rel="next" title="34.3. winreg --- Windows registry access" href="winreg.html" />
    <link rel="prev" title="34.1. msilib --- Read and write Microsoft Installer files" href="msilib.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
    <link rel="canonical" href="https://docs.python.org/3/library/msvcrt.html" />
    
    <script type="text/javascript" src="../_static/copybutton.js"></script>
    <script type="text/javascript" src="../_static/switchers.js"></script>
    
    
 

  </head><body>  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">索引</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python 模块索引"
             >模块</a> |</li>
        <li class="right" >
          <a href="winreg.html" title="34.3. winreg --- Windows registry access"
             accesskey="N">下一页</a> |</li>
        <li class="right" >
          <a href="msilib.html" title="34.1. msilib --- Read and write Microsoft Installer files"
             accesskey="P">上一页</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <span class="language_switcher_placeholder">zh</span>
          <span class="version_switcher_placeholder">3.6.6rc1</span>
          <a href="../index.html">Documentation </a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="windows.html" accesskey="U">34. MS Windows Specific Services</a> &#187;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="Quick search" type="text" name="q" />
          <input type="submit" value="Go" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>    

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="module-msvcrt">
<span id="msvcrt-useful-routines-from-the-ms-vc-runtime"></span><h1>34.2. <a class="reference internal" href="#module-msvcrt" title="msvcrt: Miscellaneous useful routines from the MS VC++ runtime. (Windows)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">msvcrt</span></code></a> --- Useful routines from the MS VC++ runtime<a class="headerlink" href="#module-msvcrt" title="永久链接至标题">¶</a></h1>
<hr class="docutils" />
<p>These functions provide access to some useful capabilities on Windows platforms.
Some higher-level modules use these functions to build the  Windows
implementations of their services.  For example, the <a class="reference internal" href="getpass.html#module-getpass" title="getpass: Portable reading of passwords and retrieval of the userid."><code class="xref py py-mod docutils literal notranslate"><span class="pre">getpass</span></code></a> module uses
this in the implementation of the <a class="reference internal" href="getpass.html#module-getpass" title="getpass: Portable reading of passwords and retrieval of the userid."><code class="xref py py-func docutils literal notranslate"><span class="pre">getpass()</span></code></a> function.</p>
<p>Further documentation on these functions can be found in the Platform API
documentation.</p>
<p>The module implements both the normal and wide char variants of the console I/O
api. The normal API deals only with ASCII characters and is of limited use
for internationalized applications. The wide char API should be used where
ever possible.</p>
<div class="versionchanged">
<p><span class="versionmodified">在 3.3 版更改: </span>Operations in this module now raise <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> where <a class="reference internal" href="exceptions.html#IOError" title="IOError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">IOError</span></code></a>
was raised.</p>
</div>
<div class="section" id="file-operations">
<span id="msvcrt-files"></span><h2>34.2.1. File Operations<a class="headerlink" href="#file-operations" title="永久链接至标题">¶</a></h2>
<dl class="function">
<dt id="msvcrt.locking">
<code class="descclassname">msvcrt.</code><code class="descname">locking</code><span class="sig-paren">(</span><em>fd</em>, <em>mode</em>, <em>nbytes</em><span class="sig-paren">)</span><a class="headerlink" href="#msvcrt.locking" title="永久链接至目标">¶</a></dt>
<dd><p>Lock part of a file based on file descriptor <em>fd</em> from the C runtime.  Raises
<a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> on failure.  The locked region of the file extends from the
current file position for <em>nbytes</em> bytes, and may continue beyond the end of the
file.  <em>mode</em> must be one of the <code class="xref py py-const docutils literal notranslate"><span class="pre">LK_*</span></code> constants listed below. Multiple
regions in a file may be locked at the same time, but may not overlap.  Adjacent
regions are not merged; they must be unlocked individually.</p>
</dd></dl>

<dl class="data">
<dt id="msvcrt.LK_LOCK">
<code class="descclassname">msvcrt.</code><code class="descname">LK_LOCK</code><a class="headerlink" href="#msvcrt.LK_LOCK" title="永久链接至目标">¶</a></dt>
<dt id="msvcrt.LK_RLCK">
<code class="descclassname">msvcrt.</code><code class="descname">LK_RLCK</code><a class="headerlink" href="#msvcrt.LK_RLCK" title="永久链接至目标">¶</a></dt>
<dd><p>Locks the specified bytes. If the bytes cannot be locked, the program
immediately tries again after 1 second.  If, after 10 attempts, the bytes cannot
be locked, <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> is raised.</p>
</dd></dl>

<dl class="data">
<dt id="msvcrt.LK_NBLCK">
<code class="descclassname">msvcrt.</code><code class="descname">LK_NBLCK</code><a class="headerlink" href="#msvcrt.LK_NBLCK" title="永久链接至目标">¶</a></dt>
<dt id="msvcrt.LK_NBRLCK">
<code class="descclassname">msvcrt.</code><code class="descname">LK_NBRLCK</code><a class="headerlink" href="#msvcrt.LK_NBRLCK" title="永久链接至目标">¶</a></dt>
<dd><p>Locks the specified bytes. If the bytes cannot be locked, <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> is
raised.</p>
</dd></dl>

<dl class="data">
<dt id="msvcrt.LK_UNLCK">
<code class="descclassname">msvcrt.</code><code class="descname">LK_UNLCK</code><a class="headerlink" href="#msvcrt.LK_UNLCK" title="永久链接至目标">¶</a></dt>
<dd><p>Unlocks the specified bytes, which must have been previously locked.</p>
</dd></dl>

<dl class="function">
<dt id="msvcrt.setmode">
<code class="descclassname">msvcrt.</code><code class="descname">setmode</code><span class="sig-paren">(</span><em>fd</em>, <em>flags</em><span class="sig-paren">)</span><a class="headerlink" href="#msvcrt.setmode" title="永久链接至目标">¶</a></dt>
<dd><p>Set the line-end translation mode for the file descriptor <em>fd</em>. To set it to
text mode, <em>flags</em> should be <a class="reference internal" href="os.html#os.O_TEXT" title="os.O_TEXT"><code class="xref py py-const docutils literal notranslate"><span class="pre">os.O_TEXT</span></code></a>; for binary, it should be
<a class="reference internal" href="os.html#os.O_BINARY" title="os.O_BINARY"><code class="xref py py-const docutils literal notranslate"><span class="pre">os.O_BINARY</span></code></a>.</p>
</dd></dl>

<dl class="function">
<dt id="msvcrt.open_osfhandle">
<code class="descclassname">msvcrt.</code><code class="descname">open_osfhandle</code><span class="sig-paren">(</span><em>handle</em>, <em>flags</em><span class="sig-paren">)</span><a class="headerlink" href="#msvcrt.open_osfhandle" title="永久链接至目标">¶</a></dt>
<dd><p>Create a C runtime file descriptor from the file handle <em>handle</em>.  The <em>flags</em>
parameter should be a bitwise OR of <a class="reference internal" href="os.html#os.O_APPEND" title="os.O_APPEND"><code class="xref py py-const docutils literal notranslate"><span class="pre">os.O_APPEND</span></code></a>, <a class="reference internal" href="os.html#os.O_RDONLY" title="os.O_RDONLY"><code class="xref py py-const docutils literal notranslate"><span class="pre">os.O_RDONLY</span></code></a>,
and <a class="reference internal" href="os.html#os.O_TEXT" title="os.O_TEXT"><code class="xref py py-const docutils literal notranslate"><span class="pre">os.O_TEXT</span></code></a>.  The returned file descriptor may be used as a parameter
to <a class="reference internal" href="os.html#os.fdopen" title="os.fdopen"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.fdopen()</span></code></a> to create a file object.</p>
</dd></dl>

<dl class="function">
<dt id="msvcrt.get_osfhandle">
<code class="descclassname">msvcrt.</code><code class="descname">get_osfhandle</code><span class="sig-paren">(</span><em>fd</em><span class="sig-paren">)</span><a class="headerlink" href="#msvcrt.get_osfhandle" title="永久链接至目标">¶</a></dt>
<dd><p>Return the file handle for the file descriptor <em>fd</em>.  Raises <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a> if
<em>fd</em> is not recognized.</p>
</dd></dl>

</div>
<div class="section" id="console-i-o">
<span id="msvcrt-console"></span><h2>34.2.2. Console I/O<a class="headerlink" href="#console-i-o" title="永久链接至标题">¶</a></h2>
<dl class="function">
<dt id="msvcrt.kbhit">
<code class="descclassname">msvcrt.</code><code class="descname">kbhit</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#msvcrt.kbhit" title="永久链接至目标">¶</a></dt>
<dd><p>Return true if a keypress is waiting to be read.</p>
</dd></dl>

<dl class="function">
<dt id="msvcrt.getch">
<code class="descclassname">msvcrt.</code><code class="descname">getch</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#msvcrt.getch" title="永久链接至目标">¶</a></dt>
<dd><p>Read a keypress and return the resulting character as a byte string.
Nothing is echoed to the console.  This call will block if a keypress
is not already available, but will not wait for <kbd class="kbd docutils literal notranslate">Enter</kbd> to be
pressed. If the pressed key was a special function key, this will
return <code class="docutils literal notranslate"><span class="pre">'\000'</span></code> or <code class="docutils literal notranslate"><span class="pre">'\xe0'</span></code>; the next call will return the keycode.
The <kbd class="kbd docutils literal notranslate">Control-C</kbd> keypress cannot be read with this function.</p>
</dd></dl>

<dl class="function">
<dt id="msvcrt.getwch">
<code class="descclassname">msvcrt.</code><code class="descname">getwch</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#msvcrt.getwch" title="永久链接至目标">¶</a></dt>
<dd><p>Wide char variant of <a class="reference internal" href="#msvcrt.getch" title="msvcrt.getch"><code class="xref py py-func docutils literal notranslate"><span class="pre">getch()</span></code></a>, returning a Unicode value.</p>
</dd></dl>

<dl class="function">
<dt id="msvcrt.getche">
<code class="descclassname">msvcrt.</code><code class="descname">getche</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#msvcrt.getche" title="永久链接至目标">¶</a></dt>
<dd><p>Similar to <a class="reference internal" href="#msvcrt.getch" title="msvcrt.getch"><code class="xref py py-func docutils literal notranslate"><span class="pre">getch()</span></code></a>, but the keypress will be echoed if it  represents a
printable character.</p>
</dd></dl>

<dl class="function">
<dt id="msvcrt.getwche">
<code class="descclassname">msvcrt.</code><code class="descname">getwche</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#msvcrt.getwche" title="永久链接至目标">¶</a></dt>
<dd><p>Wide char variant of <a class="reference internal" href="#msvcrt.getche" title="msvcrt.getche"><code class="xref py py-func docutils literal notranslate"><span class="pre">getche()</span></code></a>, returning a Unicode value.</p>
</dd></dl>

<dl class="function">
<dt id="msvcrt.putch">
<code class="descclassname">msvcrt.</code><code class="descname">putch</code><span class="sig-paren">(</span><em>char</em><span class="sig-paren">)</span><a class="headerlink" href="#msvcrt.putch" title="永久链接至目标">¶</a></dt>
<dd><p>Print the byte string <em>char</em> to the console without buffering.</p>
</dd></dl>

<dl class="function">
<dt id="msvcrt.putwch">
<code class="descclassname">msvcrt.</code><code class="descname">putwch</code><span class="sig-paren">(</span><em>unicode_char</em><span class="sig-paren">)</span><a class="headerlink" href="#msvcrt.putwch" title="永久链接至目标">¶</a></dt>
<dd><p>Wide char variant of <a class="reference internal" href="#msvcrt.putch" title="msvcrt.putch"><code class="xref py py-func docutils literal notranslate"><span class="pre">putch()</span></code></a>, accepting a Unicode value.</p>
</dd></dl>

<dl class="function">
<dt id="msvcrt.ungetch">
<code class="descclassname">msvcrt.</code><code class="descname">ungetch</code><span class="sig-paren">(</span><em>char</em><span class="sig-paren">)</span><a class="headerlink" href="#msvcrt.ungetch" title="永久链接至目标">¶</a></dt>
<dd><p>Cause the byte string <em>char</em> to be &quot;pushed back&quot; into the console buffer;
it will be the next character read by <a class="reference internal" href="#msvcrt.getch" title="msvcrt.getch"><code class="xref py py-func docutils literal notranslate"><span class="pre">getch()</span></code></a> or <a class="reference internal" href="#msvcrt.getche" title="msvcrt.getche"><code class="xref py py-func docutils literal notranslate"><span class="pre">getche()</span></code></a>.</p>
</dd></dl>

<dl class="function">
<dt id="msvcrt.ungetwch">
<code class="descclassname">msvcrt.</code><code class="descname">ungetwch</code><span class="sig-paren">(</span><em>unicode_char</em><span class="sig-paren">)</span><a class="headerlink" href="#msvcrt.ungetwch" title="永久链接至目标">¶</a></dt>
<dd><p>Wide char variant of <a class="reference internal" href="#msvcrt.ungetch" title="msvcrt.ungetch"><code class="xref py py-func docutils literal notranslate"><span class="pre">ungetch()</span></code></a>, accepting a Unicode value.</p>
</dd></dl>

</div>
<div class="section" id="other-functions">
<span id="msvcrt-other"></span><h2>34.2.3. Other Functions<a class="headerlink" href="#other-functions" title="永久链接至标题">¶</a></h2>
<dl class="function">
<dt id="msvcrt.heapmin">
<code class="descclassname">msvcrt.</code><code class="descname">heapmin</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#msvcrt.heapmin" title="永久链接至目标">¶</a></dt>
<dd><p>Force the <code class="xref c c-func docutils literal notranslate"><span class="pre">malloc()</span></code> heap to clean itself up and return unused blocks to
the operating system.  On failure, this raises <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a>.</p>
</dd></dl>

</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../contents.html">內容目录</a></h3>
  <ul>
<li><a class="reference internal" href="#">34.2. <code class="docutils literal notranslate"><span class="pre">msvcrt</span></code> --- Useful routines from the MS VC++ runtime</a><ul>
<li><a class="reference internal" href="#file-operations">34.2.1. File Operations</a></li>
<li><a class="reference internal" href="#console-i-o">34.2.2. Console I/O</a></li>
<li><a class="reference internal" href="#other-functions">34.2.3. Other Functions</a></li>
</ul>
</li>
</ul>

  <h4>上一个主题</h4>
  <p class="topless"><a href="msilib.html"
                        title="上一章">34.1. <code class="docutils literal notranslate"><span class="pre">msilib</span></code> --- Read and write Microsoft Installer files</a></p>
  <h4>下一个主题</h4>
  <p class="topless"><a href="winreg.html"
                        title="下一章">34.3. <code class="docutils literal notranslate"><span class="pre">winreg</span></code> --- Windows registry access</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../bugs.html">Report a Bug</a></li>
      <li>
        <a href="https://github.com/python/cpython/blob/3.6/Doc/library/msvcrt.rst"
            rel="nofollow">Show Source
        </a>
      </li>
    </ul>
  </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >索引</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python 模块索引"
             >模块</a> |</li>
        <li class="right" >
          <a href="winreg.html" title="34.3. winreg --- Windows registry access"
             >下一页</a> |</li>
        <li class="right" >
          <a href="msilib.html" title="34.1. msilib --- Read and write Microsoft Installer files"
             >上一页</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <span class="language_switcher_placeholder">zh</span>
          <span class="version_switcher_placeholder">3.6.6rc1</span>
          <a href="../index.html">Documentation </a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="windows.html" >34. MS Windows Specific Services</a> &#187;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="Quick search" type="text" name="q" />
          <input type="submit" value="Go" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>  
    <div class="footer">
    &copy; <a href="../copyright.html">Copyright</a> 2001-2018, Python Software Foundation.
    <br />
    The Python Software Foundation is a non-profit corporation.
    <a href="https://www.python.org/psf/donations/">Please donate.</a>
    <br />
    Last updated on 6月 24, 2018.
    <a href="../bugs.html">Found a bug</a>?
    <br />
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.7.2.
    </div>

  </body>
</html>